package com.youkeda.yunmusic.web.control;

import com.youkeda.yunmusic.web.exception.CustomizeErrorCode;
import com.youkeda.yunmusic.web.exception.CustomizeException;
import com.youkeda.yunmusic.web.model.Comment;
import com.youkeda.yunmusic.web.model.SongInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SongInfoControl {

    @Autowired
    private CommentPostControl commentPostControl;

    private static Map<String,SongInfo> songInfoMap = new HashMap<>();

    @RequestMapping(value = "/songinfo")
    public String index(String songId, ModelMap modelMap){
        if (songId==null){
            songId = "35847388";
        }
        if (songId.equals("35847388")||songId.equals("16435049")){

        SongInfo songInfo = songInfoMap.get(songId);
        modelMap.addAttribute("song",songInfo);

        List<Comment> comments = commentPostControl.getComments(songId);
        modelMap.addAttribute("comments",comments);

        return "index";
        }else {
            throw new CustomizeException(CustomizeErrorCode.SONG_NOT_FOUND);
        }
    }

    @RequestMapping(value="/songinfo/get")
    @ResponseBody
    public SongInfo get(String songId){
        SongInfo songInfo = songInfoMap.get(songId);
        return songInfo;
    }

    @PostConstruct
    public void init(){

        SongInfo songInfo = new SongInfo();
        songInfo.setId("35847388");
        songInfo.setName("Hello");
        songInfo.setSinger("Adele");
        songInfo.setAlbumName("Hello");
        songInfo.setCommentCount(94761);
        songInfo.setLyrics("Hello, it`s me<br /> " +
                "I was wondering if after all these years you`d like to meet<br /> " +
                "To go over everything<br /> " +
                "They say that time`s supposed to heal<br />");
        songInfo.setAlbumImg("http://p1.music.126.net/PrO7oPvnjvH4xdujdGbf2w==/1407374890649284.jpg?param=130y130");
        songInfoMap.put("35847388",songInfo);

        songInfo = new SongInfo();
        songInfo.setId("16435049");
        songInfo.setName("Someone Like You");
        songInfo.setSinger("Adele");
        songInfo.setAlbumName("Someone Like You");
        songInfo.setCommentCount(71202);
        songInfo.setLyrics("I heard<br /> " +
                "That you are settled down<br /> " +
                "That you found a girl<br /> " +
                "And your married now<br />");
        songInfo.setAlbumImg("http://p2.music.126.net/-g2ieFBmoy7T8crwFaAdxQ==/109951163077007716.jpg?param=130y130");
        songInfoMap.put("16435049",songInfo);

    }
}
